﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CANO.Data.DAOs
{
    public class ProfesionDAO
    {
        public int TipoProfesionID { get; set; }
        public string ProfesionDesc { get; set; }


        public static ProfesionDAO GetById(int profesionId)
        {
            try 
	            {	        
		             using (dcCanoDataContext context = new dcCanoDataContext())
                        {
                            var profesion =(
                                from c in context.TipoProfesions
                                where c.TipoProfesionID == profesionId
                                select c).SingleOrDefault();
                            if (profesion == null)
                                return new ProfesionDAO();

                            return new ProfesionDAO() {TipoProfesionID = profesion.TipoProfesionID, ProfesionDesc = profesion.Profesion};
                        }
	            }
	            catch (Exception)
	            {
                    return new ProfesionDAO();
	            }
           
        }

        public static List<ProfesionDAO> getAll()
        {
            using (dcCanoDataContext context = new dcCanoDataContext())
            {
                List<ProfesionDAO> profesionList = new List<ProfesionDAO>();

                var profesion =
                    from c in context.TipoProfesions
                    select c;

                foreach (var item in profesion)
                {
                    ProfesionDAO objProfesion = new ProfesionDAO();
                    objProfesion.TipoProfesionID = item.TipoProfesionID;
                    objProfesion.ProfesionDesc = item.Profesion;
                    profesionList.Add(objProfesion);
                }
                return profesionList;
            }
        }

        public static List<ProfesionDAO> GetProfesionByName(string Profesion)
        {
            using (dcCanoDataContext context = new dcCanoDataContext())
            {
                List<ProfesionDAO> profesionList = new List<ProfesionDAO>();

                var profesion =
                    from c in context.TipoProfesions
                    where c.Profesion == Profesion
                    select c;

                foreach (var item in profesion)
                {
                    ProfesionDAO objProfesion = new ProfesionDAO();
                    objProfesion.TipoProfesionID = item.TipoProfesionID;
                    objProfesion.ProfesionDesc = item.Profesion;
                    profesionList.Add(objProfesion);
                }
                return profesionList;
            }
        }

        public static void create(string pEspecialidad)
        {
            using (dcCanoDataContext context = new dcCanoDataContext())
            {
                TipoProfesion newProfesion = new TipoProfesion();
                newProfesion.Profesion = pEspecialidad;
                newProfesion.TipoProfesionID = getId();

                context.TipoProfesions.InsertOnSubmit(newProfesion);
                context.SubmitChanges();
            }
        }

        public static int createNew(string pEspecialidad)
        {
            using (dcCanoDataContext context = new dcCanoDataContext())
            {
                TipoProfesion newProfesion = new TipoProfesion();
                newProfesion.Profesion = pEspecialidad;
                newProfesion.TipoProfesionID = getId();

                context.TipoProfesions.InsertOnSubmit(newProfesion);
                context.SubmitChanges();

                return newProfesion.TipoProfesionID;
            }
        }

        private static int getId()
        {
            using (dcCanoDataContext context = new dcCanoDataContext())
            {
                var tipoProfesionColl =
                    from tt in context.TipoProfesions
                    select tt.TipoProfesionID;

                return tipoProfesionColl.Max() + 1;
            }
        }

        public static int? GetIdByName(string profesionToValidate)
        {
            using (dcCanoDataContext context = new dcCanoDataContext())
            {
                var profesionColl =
                    (from tp in context.TipoProfesions
                     where tp.Profesion == profesionToValidate
                     select tp);

                return profesionColl.Single().TipoProfesionID;
            };
        }

        public static void Update(ProfesionDAO tpUpdate)
        {
            using (dcCanoDataContext context = new dcCanoDataContext())
            {
                var tp =
                    (from t in context.TipoProfesions
                     where t.TipoProfesionID == tpUpdate.TipoProfesionID
                     select t).Single();

                tp.Profesion = tpUpdate.ProfesionDesc;

                context.SubmitChanges();
            }
        }

        public static int Delete(int tpUpdateId)
        {
            try
            {
                using (dcCanoDataContext context = new dcCanoDataContext())
                {
                    var tp =
                        (from t in context.TipoProfesions
                         where t.TipoProfesionID == tpUpdateId
                         select t).Single();

                    context.TipoProfesions.DeleteOnSubmit(tp);
                    context.SubmitChanges();
                    return 1;
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }

    }
}
